/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package tbenglish_client.form;

import bllfactory.SBLLFactory;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.rmi.RemoteException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import model.ClientModel;
/**
 *
 * @author Mrkupi
 */
public class ChangePassword extends javax.swing.JFrame {

    /**
     * Creates new form ChangePass
     */
    public ChangePassword() {
        initComponents();
        init();
    }
    
    private void init()
    {
        setResizable(false);
        
        setLocation( 500, 250 );
        this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
        this.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
               dispose();
            }
        });
    }
    
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        btnChangePassword = new javax.swing.JButton();
        pwfNew = new javax.swing.JPasswordField();
        pwfOld = new javax.swing.JPasswordField();
        jLabel1 = new javax.swing.JLabel();
        pwfConfirm = new javax.swing.JPasswordField();
        jLabel4 = new javax.swing.JLabel();
        pwfOld1 = new javax.swing.JPasswordField();
        jLabel5 = new javax.swing.JLabel();
        pwfNew1 = new javax.swing.JPasswordField();
        jLabel6 = new javax.swing.JLabel();
        pwfConfirm1 = new javax.swing.JPasswordField();
        btnChangePassword1 = new javax.swing.JButton();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        btnChangePassword2 = new javax.swing.JButton();
        pO = new javax.swing.JPasswordField();
        pN = new javax.swing.JPasswordField();
        pC = new javax.swing.JPasswordField();

        jLabel2.setText("New Password");

        jLabel3.setText("Confirm");

        btnChangePassword.setText("Change Password");
        btnChangePassword.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnChangePasswordActionPerformed(evt);
            }
        });

        jLabel1.setText("Old Password");

        jLabel4.setText("Old Password");

        jLabel5.setText("New Password");

        jLabel6.setText("Confirm");

        btnChangePassword1.setText("Change Password");
        btnChangePassword1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnChangePassword1ActionPerformed(evt);
            }
        });

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel7.setText("Old Password");

        jLabel8.setText("New Password");

        jLabel9.setText("Confirm");

        btnChangePassword2.setText("Change");
        btnChangePassword2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnChangePassword2ActionPerformed(evt);
            }
        });

        pO.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                pOActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel8)
                    .addComponent(jLabel7)
                    .addComponent(jLabel9, javax.swing.GroupLayout.Alignment.TRAILING))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 16, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(pC, javax.swing.GroupLayout.PREFERRED_SIZE, 216, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addComponent(pN, javax.swing.GroupLayout.DEFAULT_SIZE, 216, Short.MAX_VALUE)
                        .addComponent(pO)))
                .addContainerGap(32, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(btnChangePassword2)
                .addGap(136, 136, 136))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel7)
                            .addComponent(pO, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(26, 26, 26)
                        .addComponent(jLabel8))
                    .addComponent(pN, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel9)
                    .addComponent(pC, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(btnChangePassword2)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnChangePasswordActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnChangePasswordActionPerformed
        
    }//GEN-LAST:event_btnChangePasswordActionPerformed

    private void btnChangePassword1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnChangePassword1ActionPerformed
       
    }//GEN-LAST:event_btnChangePassword1ActionPerformed

    private void btnChangePassword2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnChangePassword2ActionPerformed
        // TODO add your handling code here:
        String oldPassword = String.valueOf( this.pO.getPassword() );
        String newPassword = String.valueOf( this.pN.getPassword() );
        String confirm = String.valueOf( this.pC.getPassword() );

        if( makeSureAllFields() )
        {
            return;
        }
        try {
            if(SBLLFactory.getInstance().getAccount().changePassword(ClientModel.getInstance().getAccount().getUserID(), oldPassword, newPassword))
            {
                JOptionPane.showMessageDialog( this, "Success" );
                this.dispose();
            }
        } catch (RemoteException ex) {
            Logger.getLogger(ChangePassword.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_btnChangePassword2ActionPerformed

    private void pOActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_pOActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_pOActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnChangePassword;
    private javax.swing.JButton btnChangePassword1;
    private javax.swing.JButton btnChangePassword2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPasswordField pC;
    private javax.swing.JPasswordField pN;
    private javax.swing.JPasswordField pO;
    private javax.swing.JPasswordField pwfConfirm;
    private javax.swing.JPasswordField pwfConfirm1;
    private javax.swing.JPasswordField pwfNew;
    private javax.swing.JPasswordField pwfNew1;
    private javax.swing.JPasswordField pwfOld;
    private javax.swing.JPasswordField pwfOld1;
    // End of variables declaration//GEN-END:variables

    
    private boolean makeSureAllFields()
    {
        //NEED TABLE
        String oldPassword = String.valueOf( this.pO.getPassword() );
        String newPassword = String.valueOf( this.pN.getPassword() );
        String confirm = String.valueOf( this.pC.getPassword() );
        if("".equals(oldPassword) || "".equals(newPassword))
        {
            JOptionPane.showMessageDialog( this, "You must enter all fields" );
            return true;
        }
        
        if(!newPassword.equals(confirm))
        {
            JOptionPane.showMessageDialog( this, "Confirm password must correct with new password" );
            return true;
        }
        
        if(newPassword.equals(oldPassword))
        {
            JOptionPane.showMessageDialog( this, "New password had to different with old password" );
            return true;
        }
        
        return false;
    }
}
